#include "card.h"

int card_suit(card c) {
    return c.order / RANK_COUNT;
}

int card_rank(card c) {
    return c.order % RANK_COUNT;
}

card gen_card(int suit, int rank) {
    return (card){suit * RANK_COUNT + rank};
}

void card_name(char* str, card c) {
    char rank_name[10];
    card_rank_name(rank_name, card_rank(c));
    char suit_name[10];
    card_suit_name(suit_name, card_suit(c));
    str = strcpy(str, suit_name);
    str = strcat(str, " ");
    str = strcat(str, rank_name);
    return;
}

void card_swap(card* a, card* b) {
    card tmp = *a;
    *a = *b;
    *b = tmp;
}

bool card_is_special(card c) {
    int r = card_rank(c);
    return r == 0 || r == 1 || r == 5 || r == 9 || r == 10;
}

int card_need_draw(card c) {
    int r = card_rank(c);
    return r < 2 ? r + 2 : 0;
}

bool card_is_cancel(card c) {
    return card_rank(c) == 5;
}

bool card_is_skip(card c) {
    return card_rank(c) == 9;
}

bool card_is_rev(card c) {
    return card_rank(c) == 10;
}

void card_rank_name(char* str, int rank) {
    switch (rank) {
        case 8:
            strcpy(str, "10");
            break;
        case 9:
            strcpy(str, "Jack");
            break;
        case 10:
            strcpy(str, "Queen");
            break;
        case 11:
            strcpy(str, "King");
            break;
        case 12:
            strcpy(str, "Ace");
            break;

        default:
            str[0] = '2' + rank;
            str[1] = '\0';
            break;
    }
}

void card_suit_name(char* str, int suit) {
    switch (suit) {
        case 0:
            strcpy(str, "Spades");
            break;
        case 1:
            strcpy(str, "Hearts");
            break;
        case 2:
            strcpy(str, "Diamonds");
            break;
        case 3:
            strcpy(str, "Clubs");
            break;

        default:
            break;
    }
}
